<?php
class TTF_Form_Food extends Zend_Form
{
	public function init()
	{
		$this->setAction('/admin/thuc-pham/them')
				->setMethod('post');
				
		// create text input for name
		$name = new Zend_Form_Element_Text('name');
		$name->setLabel('Tên thực phẩm:')
				->setOptions(array('size' => '30'))
				->setRequired(true);
				
		// create select input for category
		$categories = new Zend_Form_Element_Select('category_id');
		$categories->setLabel('Chọn danh mục:')
				->addMultiOption('', 'None');
		foreach ($this->getCategories() as $category) {
			$categories->addMultiOption($category['id'], $category['name']);
		}
		
		// create text input for price
		$price = new Zend_Form_Element_Text('price');
		$price->setLabel('Giá thực phẩm:')
				->setRequired(true)
				->setOptions(array('size' => '30'));
				
		// create select input for currency
		$currency = new Zend_Form_Element_Select('currency_id');
		$currency->setLabel('Chọn đơn vị tiền:')
					->addMultiOption('', 'None');
		foreach ($this->getCurrency() as $cr) {
			$currency->addMultiOption($cr['id'], $cr['name']);
		}
		
		// create select input for unit
		$unit = new Zend_Form_Element_Select('unit_id');
		$unit->setLabel('Chọn đơn vị tính:')
				->addMultiOption('', 'None');
		foreach ($this->getUnit() as $u){
			$unit->addMultiOption($u['id'], $u['name']);
		}
		
		// create file input for image
		$image = new Zend_Form_Element_File('image');
		$image->setLabel('Chọn hình cho thực phẩm:');

		// create text input for description
		$description = new Zend_Form_Element_Textarea('description');
		$description->setLabel('Mô tả chi tiết thực phẩm:')
					->setOptions(array('rows' => '50', 'cols' => '113'))
					->setRequired(true);
					
		// create submit button
		// create submit button
		$submit = new Zend_Form_Element_Submit('submit');
		$submit->setLabel('Thêm')
			->setOptions(array('class' => 'loginbutton'));
			
		// attach elements to form
		$this->addElement($name)
			->addElement($categories)
			->addElement($price)
			->addElement($currency)
			->addElement($unit)
			->addElement($image)
			->addElement($description)
			->addElement($submit);
					
	}
	
	public function getCategories() 
	{
		$q = Doctrine_Query::create()
			->from('TTF_Model_Categories c');						
		$results = $q->fetchArray();
		if (count($results) > 0) {
			$x = 0;
			$list = array(array());
			foreach ($results as $result) {
				$q = Doctrine_Query::create()
						->from('TTF_Model_Foods f')
						->where('f.category_id = ?', $result['id']);
				if (count($q->fetchArray()) > 0) {
					$list[$x]['id'] = $result['id'];
					$list[$x]['name'] = $result['name'];
					$x++;
				}
			}
			return $list;
		}
	}
	
	public function getCurrency()
	{
		$q = Doctrine_Query::create()
				->from('TTF_Model_Currency c');
		return $q->fetchArray();
	}
	
	public function getUnit()
	{
		$q = Doctrine_Query::create()
			->from('TTF_Model_Unit u');
		return $q->fetchArray();
	}
}